package com.neusoft.SPNursingHome.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.converter.BeanOutputConverter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.SPNursingHome.po.Food;
@RestController
public class BeanOutputController {
    private final ChatClient chatClient;

    private final BeanOutputConverter<Food> fconverter;

    public BeanOutputController(ChatClient.Builder builder) {
        super();
        chatClient=builder.build();
        // TODO Auto-generated constructor stub
        fconverter=new BeanOutputConverter<>(Food.class);

    }
    @GetMapping("/food")
    public Food getFood(@RequestParam(value="query",defaultValue = "请介绍一种食物，要求提供食物名称、类型、热量（卡路里）")String query) {
        Prompt prompt=new Prompt(query);
//      AI输出提示模板
        String promptTemplate="""
            format: 以纯文本输出 json，请不要包含任何多余的文字——包括 markdown 格式;
            outputExample: {
           "foodName": {foodName},
           "foodType": {foodType},
           "calories": {calories},
      };
                """;
        String json=chatClient.prompt(prompt).user(u->{
            u.text(promptTemplate);
        }).call().content();
        System.out.println(json);
//      调用结构化输出转换器，把json变成food对象
        Food food=fconverter.convert(json);
        return food;
    }



}
